checkLogin();

//获取水票详情及其所选套餐信息
var id = GetQueryString('id');
var index = GetQueryString('index'); //表示套餐id
// console.log(id + '==' + index);
var waterbill_id = id;
var bill_id = index;

// getData(address_list, comParams(), success);
var key_text = getCache(login_key);
var sid = key_text.sid;
var mid = key_text.mid;
var success = function (res) {
    var info = res.data.info;
    if (info != undefined && info != null && info != '') {
        if (info.waterbill_category != undefined && info.waterbill_category != null && info.waterbill_category != '' && info.waterbill_category.length > 0) {
            for (var i = 0; i < info.waterbill_category.length; i++) {
                var choose = info.waterbill_category[i];
                if (choose.id == index) {
                    info.waterbill_category[i].choosed = true;
                    bill_id = choose.id;
                }
            }
            waterbill_id = info.id;
        }
    }
    var html = template('water_ticket_detail', info);     //order 模板id
    $('#ticket_detail_content').append(html);

    //实付 和 合计模板

    html = template('need_pay', info);
    $('#need_pay_content').append(html);

    html = template('all_need_pay', info);
    $('#all_need_pay_content').append(html);

    // var order_success = getCache(mid + "_" + sid + '_' + 'order');
    // if (order_success == 1) {
    //     setCache('confirm', false);
    //     checkFrim();
    // } else {
    //     var success = function (res_new) {
    //         var list = res_new.data.order_list.data;
    //         var count = 0;
    //         setCache(mid + "_" + sid + '_' + 'order', 0);
    //         setCache('confirm', true);
    //         if (!isEmpty(list) && list.length > 0) {
    //             for (var i = 0; i < list.length; i++) {
    //                 if (list[i].status == 20) {
    //                     count++;
    //                     break
    //                 }
    //             }
    //         }
    //         if (count > 0) {
    //             setCache(mid + "_" + sid + '_' + 'order', 1);
    //             setCache('confirm', false);
    //         }
    //         checkFrim();
    //     }
    //     getData(order_index, combineParams({type: 1}), success);
    // }
    // function checkFrim() {
    //     var ajaxOption = {
    //         url: waterbill_pay,
    //         method: 'POST',
    //         confirm: getCache('confirm'),
    //         confirm_title: '提示',
    //         confirm_content: '首次订水，需付桶押金，送水工线下收取，押金可退。',//用[]包起来表示该值是函数，不包则为字符串
    //         data: combineParams({bill_id: bill_id, waterbill_id: waterbill_id}),
    //         success: function (res) {
    //             payment(res.data);
    //         },
    //         error: function (res) {
    //             setTimeout(function(){
    //                 locationReplace('fail.html?type=2');
    //             },3000);
    //         }
    //     };
    //     checkform($('#orderForm'), ajaxOption);
    // }

}
getData(waterbill_info, combineParams({'id': id}), success);


//支付方式
var success = function (res) {
    var pay_type = res.data.pay_method;
    var html = template('pay_methods', pay_type);
    $("#pay_methods_content").append(html);
    var url = '';
    if (pay_type.online_pay > 0) {
        url = waterbill_pay;
    }
    if (pay_type.online_pay == 0 && pay_type.offline_pay > 0 ) {
        url = waterbill_add;
    }
    var ajaxOption = {
        url: url,
        method: 'POST',
        confirm: getCache('confirm'),
        confirm_title: '提示',
        confirm_content: '首次订桶装水，无水桶需付桶押金，一次性桶装水无需押金，送水工线下收，可退',//用[]包起来表示该值是函数，不包则为字符串
        data: combineParams({ bill_id: bill_id, waterbill_id: waterbill_id}),
        success: function (res) {
            if (ajaxOption.url == waterbill_add) {
                locationReplace('success.html?type=2');
            } else {
                payment(res.data);
            }
        },
        error: function (res) {
            locationReplace('fail.html?type=2');
        }
    };
    $('.pay_type_wx').click(function () {
        $("#native_pay").prop('checked', false);
        $("#wx_pay").prop('checked', true);
        ajaxOption.url = waterbill_pay;
    });
    $('.pay_type_na').click(function () {
        $('#wx_pay').prop('checked', false);
        $('#native_pay').prop('checked', true);
        ajaxOption.url = waterbill_add;
    });
    check_button();

    if (pay_type.online_pay > 0 || pay_type.offline_pay > 0) {

        var order_success = getCache(mid + "_" + sid + '_' + 'order');
        if (order_success == 1) {
            setCache('confirm', false);
            checkform($('#orderForm'), ajaxOption);
        } else {
            var success = function (res_new) {
                var list = res_new.data.order_list.data;
                var count = 0;
                setCache(mid + "_" + sid + '_' + 'order', 0);
                setCache('confirm', true);
                if (!isEmpty(list) && list.length > 0) {
                    for (var i = 0; i < list.length; i++) {
                        if (list[i].status == 20) {
                            count++;
                            break
                        }
                    }
                }
                if (count > 0) {
                    setCache(mid + "_" + sid + '_' + 'order', 1);
                    setCache('confirm', false);
                }
                checkform($('#orderForm'), ajaxOption);
            }
            getData(order_index, combineParams({type: 1}), success);
        }

    }
};
getData(pay_method, comParams(), success);


function check_button() {
    var _has = false
    $('.pay_type').each(function () {
        if ($(this).prop('checked')) {
            _has = true;
        }
    });
    if (_has) {
        $('#ToBuyStep2').removeClass('mui-disabled').removeAttr('disabled');
    } else {
        $('#ToBuyStep2').addClass('mui-disabled').attr('disabled', 'true');
    }
}

//微信支付
function payment(data) {
    if (window.__wxjs_environment === 'miniprogram') {
        data.show_type = 2;
        var status = false;
        wx.miniProgram.redirectTo({
            url: '/pages/qds/wxpay/index?params=' + escape(JSON.stringify(data))
        })
        var mask = mui.createMask(function () {
            return false;
        });
        mask.show();//显示遮罩
        mui.confirm('', '是否支付成功 ?', ['是', '否'], function (e) {
            if (e.index == 0) {
                var success = function (res) {
                    if (res.data.result != undefined) {
                        status = res.data.result;
                    }
                    if (status) {
                        clearCart();
                        locationReplace('success.html?type=2');
                    } else {
                        locationReplace('fail.html?type=2');
                    }
                };
                getData(waterbill_order_check_status, combineParams({id: data.id}), success);
            } else {
                locationReplace('fail.html?type=2');
            }
        })
    } else {
        wxPay(data, 2);
    }
}
